//-----------------------------------------------------------------------
// <copyright file="CompanyAndBusinesDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class CompanyAndBusinesDAOBase
    {
        #region Common methods
        public virtual CompanyAndBusines CreateCompanyAndBusinesFromReader(IDataReader reader)
        {
            CompanyAndBusines item = new CompanyAndBusines();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("CompanyAndBusinessID"))) item.CompanyAndBusinessID = (int)reader["CompanyAndBusinessID"];
                if (!reader.IsDBNull(reader.GetOrdinal("CompanyID"))) item.CompanyID = (long)reader["CompanyID"];
                if (!reader.IsDBNull(reader.GetOrdinal("BusinessID"))) item.BusinessID = (int)reader["BusinessID"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCompanyAndBusinesFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateCompanyAndBusines methods
            
        public virtual void CreateCompanyAndBusines(CompanyAndBusines companyAndBusines)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndBusinessInsert");
                
                database.AddInParameter(dbCommand, "@CompanyID", DbType.Int64, companyAndBusines.CompanyID);
                database.AddInParameter(dbCommand, "@BusinessID", DbType.Int32, companyAndBusines.BusinessID);
                database.AddOutParameter(dbCommand, "@CompanyAndBusinessID", DbType.Int32, 4);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                companyAndBusines.CompanyAndBusinessID = (int)database.GetParameterValue(dbCommand, "@CompanyAndBusinessID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCompanyAndBusinesException, ex);
            }
        }

        #endregion

        #region UpdateCompanyAndBusines methods
        
        public virtual void UpdateCompanyAndBusines(CompanyAndBusines companyAndBusines)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndBusinessUpdate");            
                
                database.AddInParameter(dbCommand, "@CompanyAndBusinessID", DbType.Int32, companyAndBusines.CompanyAndBusinessID);
                database.AddInParameter(dbCommand, "@CompanyID", DbType.Int64, companyAndBusines.CompanyID);
                database.AddInParameter(dbCommand, "@BusinessID", DbType.Int32, companyAndBusines.BusinessID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateCompanyAndBusinesException, ex);
            }
        }
        
        #endregion

        #region DeleteCompanyAndBusines methods
        public virtual void DeleteCompanyAndBusines(int companyAndBusinessID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndBusinessDelete");
                
                database.AddInParameter(dbCommand, "@CompanyAndBusinessID", DbType.Int32, companyAndBusinessID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteCompanyAndBusinesException, ex);
            }
        }

        #endregion

        #region GetCompanyAndBusines methods
        
        public virtual CompanyAndBusines GetCompanyAndBusines(int companyAndBusinessID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndBusinessGet");
                
                database.AddInParameter(dbCommand, "@CompanyAndBusinessID", DbType.Int32, companyAndBusinessID);
                
                CompanyAndBusines companyAndBusines =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        companyAndBusines = CreateCompanyAndBusinesFromReader(reader);
                        reader.Close();
                    }
                }
                return companyAndBusines;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCompanyAndBusinesException, ex);
            }
        }
        
        #endregion

        #region GetCompanyAndBusinesList methods
        public virtual CompanyAndBusinesCollection GetCompanyAndBusinesList(CompanyAndBusinesColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndBusinessGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                CompanyAndBusinesCollection companyAndBusinesCollection = new CompanyAndBusinesCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        CompanyAndBusines companyAndBusines = CreateCompanyAndBusinesFromReader(reader);
                        companyAndBusinesCollection.Add(companyAndBusines);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return companyAndBusinesCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCompanyAndBusinesListException, ex);
            }
        }
        
        public virtual CompanyAndBusinesCollection GetCompanyAndBusinesList(CompanyAndBusinesColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetCompanyAndBusinesList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetCompanyAndBusinesList methods
        public virtual CompanyAndBusinesCollection GetCompanyAndBusinesListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndBusinessGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                CompanyAndBusinesCollection companyAndBusinesCollection = new CompanyAndBusinesCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        CompanyAndBusines companyAndBusines = CreateCompanyAndBusinesFromReader(reader);
                        companyAndBusinesCollection.Add(companyAndBusines);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return companyAndBusinesCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCompanyAndBusinesListException, ex);
            }
        }
        
        public virtual CompanyAndBusinesCollection GetCompanyAndBusinesListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetCompanyAndBusinesListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}